package com.qigao.mall.channel.weichat;

import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.channel.ChannelCore;
import com.qigao.mall.channel.config.ChannelConstant;
import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.ChannelActionEnum;
import com.qigao.mall.commons.enums.ChannelCodeEnum;
import com.qigao.mall.commons.enums.ChannelInfoEnum;
import com.qigao.mall.commons.enums.PayProductEnum;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import com.qigao.mall.commons.utils.HttpClientUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 无限制的二维码生成接口
 */
@Service
public class WeichatUnlimitQrCode implements ChannelCore {
    private static Logger logger= LoggerFactory.getLogger(WeichatUnlimitQrCode.class);
    @Resource
    ChannelConstant channelConstant;

    @Override
    public boolean isSupport(PayProductEnum payProductEnum, ChannelActionEnum channelActionEnum, ChannelInfoEnum channelInfoEnum) {
        return ChannelActionEnum.CREATE_QRCODE.equals(channelActionEnum) &&
                PayProductEnum.WEICHAT_SMALL_APP.equals(payProductEnum) &&
                ChannelInfoEnum.WEICHAT.equals(channelInfoEnum)
                ;
    }

    @Override
    public ChannelRespParam channelProcess(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        JSONObject reqContentJson=JSONObject.parseObject(channelReqParam.getReqContent());
        String apiUrl=channelConstant.getWeichatQrcodeUrl()+"?access_token="+reqContentJson.getString("accessToken")+"&scene="+reqContentJson.getString("shopNo");
        logger.info("获取微信二维码请求地址:{}",apiUrl);
        Map reqMap=new HashMap<>();
        reqMap.put("scene",reqContentJson.getString("shopNo"));
        reqMap.put("width",375);

        byte[] inputStream=HttpClientUtil.doPostByte(apiUrl,reqMap);
       // logger.info("微信接口凭证获取返回内容:{}",respContent);
        ChannelRespParam channelRespParam=new ChannelRespParam();
        channelRespParam.setRespCode(ChannelCodeEnum.FAIL.getCode());
        channelRespParam.setRespMsg(ChannelCodeEnum.FAIL.getName());
        if(inputStream!=null){
            channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
            channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
            String imageBase64Code=getBase64FromInputStream(inputStream);
            channelRespParam.setRespContent(imageBase64Code);
           /* try {
                String respContent = IOUtils.toString(inputStream, "UTF-8");
                JSONObject respJson=JSONObject.parseObject(respContent);
                if(respJson.containsKey("errcode")){
                    channelRespParam.setRespCode(ChannelCodeEnum.FAIL.getCode());
                    channelRespParam.setRespMsg(respJson.getString("errmsg"));
                }else {
                    channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
                    channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
                    String imageBase64Code=getBase64FromInputStream(inputStream);
                    channelRespParam.setRespContent(imageBase64Code);
                    if (inputStream != null) {
                        inputStream.close();
                    }
                }
            } catch (IOException e) {
                logger.error("渠道端获取微信二维码发生异常:{}",e.getMessage());
                channelRespParam.setRespMsg(e.getMessage());
            }*/
        }
        return channelRespParam;
    }

    /**
      *
      * 转base64
      *
      * */
 public static String getBase64FromInputStream(byte[] inputBytes) {
     String encodedStr = Base64.encodeBase64String(inputBytes);
     return encodedStr;
  }
}
